﻿/* Copyright 2010 Igor Francé
 * 
 * This file is part of ECMADoc (http://ecmadoc.codeplex.com).
 * 
 * ECMADoc is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * ECMADoc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with ECMADoc. 
 * If not, see http://www.gnu.org/licenses/.
 */
namespace EcmaDoc.Core.Items
{
	using System;

	/// <summary>
	/// Represents a document code item.
	/// </summary>
	public class DocumentItem : CodeItem
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="DocumentItem"/> class, using the specified
		/// <paramref name="parentItem"/> and <paramref name="name"/>.
		/// </summary>
		/// <param name="parentItem">The parent item that this code item belongs to.</param>
		/// <param name="name">The name of this document item.</param>
		public DocumentItem(CodeItem parentItem, string name)
			: this(parentItem, name, (Comment) null)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DocumentItem"/> class, using the specified
		/// <paramref name="parentItem"/>, <paramref name="name"/> and <paramref name="commentString"/>.
		/// </summary>
		/// <param name="parentItem">The parent item that this code item belongs to.</param>
		/// <param name="name">The name of this document item.</param>
		/// <param name="commentString">The comment string for this document item.</param>
		public DocumentItem(CodeItem parentItem, string name, string commentString)
			: this(parentItem, name, (Comment) null)
		{
			this.Comment = Comment.Create(commentString, this);
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DocumentItem"/> class, using the specified
		/// <paramref name="parentItem"/>, <paramref name="name"/> and <paramref name="comment"/>.
		/// </summary>
		/// <param name="parentItem">The parent item that this code item belongs to.</param>
		/// <param name="name">The name of this document item.</param>
		/// <param name="comment">The parsed comment for this document item.</param>
		public DocumentItem(CodeItem parentItem, string name, Comment comment)
			: base(parentItem, name, comment)
		{
			this.ItemType = ItemType.Document;
			this.Namespaces = new MemberDictionary();
		}

		/// <summary>
		/// Gets the name of this item.
		/// </summary>
		public override string Name
		{
			get
			{
				return FullName;
			}
		}

		/// <summary>
		/// Gets the documentation URL of this code item.
		/// </summary>
		public override string Url
		{
			get
			{
				string extension = this.OwnerProject.Info.FileExtension;
				string itemName = CodeItem.GetValidFileName(this.Name);
				return itemName + extension;
			}
		}

		/// <summary>
		/// Gets the name of this item as it will be saved when creating the XML element that represents this item.
		/// </summary>
		public override string XmlItemName
		{
			get
			{
				return FullName;
			}
		}

		/// <summary>
		/// Gets the namespaces used within this document.
		/// </summary>
		public MemberDictionary Namespaces { get; private set; }

		/// <summary>
		/// Created and adds a namespace to this document, using the specified namespace <paramref name="name"/>.
		/// </summary>
		/// <param name="name">The name of the namespace to add.</param>
		/// <returns>A <see cref="NamespaceItem"/>, created using the specified <paramref namee="name"/>.</returns>
		public NamespaceItem AddNamespace(string name)
		{
			return this.AddNamespace(name, null);
		}

		/// <summary>
		/// Created and adds a namespace to this document, using the specified namespace <paramref name="name"/> and 
		/// <paramref name="comment"/>.
		/// </summary>
		/// <param name="name">The name of the namespace to add.</param>
		/// <param name="comment">The comment string for the namespace item to create and add.</param>
		/// <returns>A <see cref="NamespaceItem"/>, created using the specified <paramref namee="name"/>.</returns>
		public NamespaceItem AddNamespace(string name, string comment)
		{
			if (this.Namespaces.ContainsKey(name))
			{
				var ns = this.Namespaces[name];
				if (ns.Comment == null && !string.IsNullOrEmpty(comment))
					ns.Comment = Comment.Create(comment, ns);

				return (NamespaceItem) ns;
			}

			var item = new NamespaceItem(this, name, comment);
			this.Namespaces.Add(name, item);
			return item;
		}
	}
}
